import 'package:boss/provider/business_setting_provider.dart';
import 'package:boss/provider/takeout_process_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'widget/takeout_process_browser.dart';

@RouterConfig(TakeoutProcessPage.pageName)
class TakeoutProcessPage extends StatefulWidget {
  static const pageName = '/TakeoutProcessPage';

  @override
  _TakeoutProcessPageState createState() => _TakeoutProcessPageState('接单外卖');
}

class _TakeoutProcessPageState extends BaseState<TakeoutProcessPage> {
  _TakeoutProcessPageState(String pageName) : super(pageName);

  @override
  void initState() {
    super.initState();

    var provider = Provider.of<TakeoutProcessProvider>(
      context,
      listen: false,
    );
    provider.resetData();
    provider.queryTakeoutOrderCount();

    queryStatus();
  }

  queryStatus() async {
    var provider = Provider.of<BusinessSettingProvider>(
      context,
      listen: false,
    );
    await provider.initData();
  }

  changeBusinessStatus() async {
    AnalyticsUtil.instance.analytics.logEvent(name: 'mqtt_recover', parameters: {'mqtt': 'test'});
    var provider = Provider.of<BusinessSettingProvider>(
      context,
      listen: false,
    );

    var content = provider.takeoutBusiness.isBusinessStatus ? '您确定闭店休息停止外卖接单?' : '您确定开店营业开始外卖接单?';

    var confirmText = provider.takeoutBusiness.isBusinessStatus ? '闭店休息' : '开始营业';
    var cancelText = provider.takeoutBusiness.isBusinessStatus ? '再营业一会儿' : '稍后营业';

    showAlert(
      context,
      content: content,
      confirmText: confirmText,
      cancelText: cancelText,
      onConfirm: (context) async {
        var tip = provider.takeoutBusiness.isBusinessStatus ? '门店已休息,明日赶早' : '已开启营业,祝您生意兴隆';
        provider.setBusinessStatus(!provider.takeoutBusiness.isBusinessStatus);
        if (await provider.update(successTip: tip)) {
          Navigator.pop(context);
        }
      },
      onCancel: (context) {
        Navigator.pop(context);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '订单处理',
        actions: <Widget>[
          Consumer<BusinessSettingProvider>(
            builder: (context, provider, _) {
              var title = provider.takeoutBusiness == null
                  ? '加载中'
                  : provider.takeoutBusiness.isBusinessStatus
                      ? '闭店休息'
                      : '开店营业';
              return LabelButton(
                title: title,
                padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                onTap: changeBusinessStatus,
              );
            },
          )
        ],
      ),
      body: TakeoutProvessBrowser(),
    );
  }
}
